﻿#region Usings

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.Security;
using XPressify.ActionResults;
using XPressify.Data;
using XPressify.Models;

#endregion

namespace XPressify.Controllers
{
    public class ImageController : BaseController
    {
        private string _imagePath;

        public ImageController(IRepository repository)
        {
            Repository = repository;
        }

        public string ImagePath
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_imagePath))
                    return _imagePath;

                if (ControllerContext == null)
                    return null;

                var context = ControllerContext.HttpContext;
                if (context == null)
                    return null;

                var server = context.Server;
                if (server == null)
                    return string.Empty;

                _imagePath = server.MapPath("~/Media/Image/SocialNetwork.jpg");
                return _imagePath;
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        public void GetImage()
        {
            new WebImage(ImagePath)
                .Write();
        }

        public ActionResult Cropped()
        {
            return View();
        }

        public WebImageResult GetCropped()
        {
            var result = new WebImageResult(
                new WebImage(ImagePath)
                    .Crop(50, 50, 50, 50)
                );
            return result;
        }

        public ActionResult HorizontalFlip()
        {
            return View();
        }

        public void GetHorizontalFlip()
        {
            new WebImage(ImagePath)
                .FlipHorizontal()
                .Write();
        }

        public ActionResult VerticalFlip()
        {
            return View();
        }

        public void GetVerticalFlip()
        {
            new WebImage(ImagePath)
                .FlipVertical()
                .Write();
        }

        public ActionResult Resized()
        {
            return View();
        }

        public void GetResized()
        {
            new WebImage(ImagePath)
                .Resize(200, 200) // resize image to 200x200 px
                .Write();
        }

        public ActionResult RotateLeft()
        {
            return View();
        }

        public void GetRotateLeft()
        {
            new WebImage(ImagePath)
                .RotateLeft()
                .Write();
        }

        public ActionResult RotateRight()
        {
            return View();
        }

        public void GetRotateRight()
        {
            new WebImage(ImagePath)
                .RotateRight()
                .Write();
        }

        public ActionResult TextWatermark()
        {
            return View();
        }

        public void GetTextWatermark()
        {
            new WebImage(ImagePath)
                .AddTextWatermark("Watermark", "black", 14, "Bold")
                .Write();
        }

        public ActionResult ImageWatermark()
        {
            return View();
        }

        public void GetImageWatermark()
        {
            var watermarkPath = HttpContext.Server.MapPath("~/Media/Image/No.jpg");
            var watermark = new WebImage(watermarkPath);

            new WebImage(ImagePath)
                .AddImageWatermark(watermark)
                .Write();
        }

        public ActionResult ImageList()
        {
            var user = CurrentUser; //
            var uploadedFiles = new List<UploadedFile>();
//            var files = Directory.GetFiles(Server.MapPath("~/Images"));
            var files = Directory.GetFiles(Server.MapPath("~/Media/" + user.Id + "/"));


            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file);
                
                var uploadedFile = new UploadedFile
                                       {
                                           Name = Path.GetFileName(file),
                                           Size = fileInfo.Length,
                                           Path = Server.MapPath("~/Media/" + user.Id + "/") + Path.GetFileName(file)
//                                           Path = Server.MapPath("~/Images/") + Path.GetFileName(file)
                                       };
                uploadedFiles.Add(uploadedFile);
            }

            return View(uploadedFiles);
        }

        public ActionResult SimpleGallery()
        {
            return View();
        }

        public ActionResult MediaDisplay()
        {
            return View();
        }
    }
}